package com.yunhe.rentcars.pro.controller.system;

import com.yunhe.rentcars.commons.dto.JSONResponse;
import com.yunhe.rentcars.commons.dto.TreeNode;
import com.yunhe.rentcars.pro.entity.system.SysDict;
import com.yunhe.rentcars.pro.service.system.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/sysdict")
public class SysDictController {
    @Autowired
    private SysDictService sysDictService;

    @RequestMapping
    public String index(HttpServletRequest request){
        List<SysDict> sysDicts = sysDictService.findList();
        request.setAttribute("sysDicts",sysDicts);
        return "/system/dictionary/sysdict";
    }

    @RequestMapping("/tree")
    @ResponseBody
    public JSONResponse tree(SysDict sysDict){
        List<TreeNode> treeNodes = sysDictService.findTreeNodes(sysDict);
        return JSONResponse.success("权限树节点获取成功",treeNodes);
    }


    @RequestMapping("/add")
    @ResponseBody
    public JSONResponse add(SysDict sysDict){
        sysDictService.save(sysDict);
        return JSONResponse.success("保存成功");
    }


    @RequestMapping("/edit")
    @ResponseBody
    public JSONResponse edit(SysDict sysDict,HttpServletRequest request){
        List<SysDict> sysDicts = sysDictService.findList();
        request.setAttribute("sysDicts",sysDicts);
        sysDictService.update(sysDict);
        return JSONResponse.success("保存成功");
    }
}
